package com.byb.server.service.impl;

import com.byb.server.service.WcInsuranceTypeService;
import com.byb.server.domain.WcInsuranceType;
import com.byb.server.repository.WcInsuranceTypeRepository;
import com.byb.server.repository.search.WcInsuranceTypeSearchRepository;
import com.byb.server.service.dto.WcInsuranceTypeDTO;
import com.byb.server.service.mapper.WcInsuranceTypeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing WcInsuranceType.
 */
@Service
@Transactional
public class WcInsuranceTypeServiceImpl implements WcInsuranceTypeService{

    private final Logger log = LoggerFactory.getLogger(WcInsuranceTypeServiceImpl.class);

    private final WcInsuranceTypeRepository wcInsuranceTypeRepository;

    private final WcInsuranceTypeMapper wcInsuranceTypeMapper;

    private final WcInsuranceTypeSearchRepository wcInsuranceTypeSearchRepository;
    public WcInsuranceTypeServiceImpl(WcInsuranceTypeRepository wcInsuranceTypeRepository, WcInsuranceTypeMapper wcInsuranceTypeMapper, WcInsuranceTypeSearchRepository wcInsuranceTypeSearchRepository) {
        this.wcInsuranceTypeRepository = wcInsuranceTypeRepository;
        this.wcInsuranceTypeMapper = wcInsuranceTypeMapper;
        this.wcInsuranceTypeSearchRepository = wcInsuranceTypeSearchRepository;
    }

    /**
     * Save a wcInsuranceType.
     *
     * @param wcInsuranceTypeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public WcInsuranceTypeDTO save(WcInsuranceTypeDTO wcInsuranceTypeDTO) {
        log.debug("Request to save WcInsuranceType : {}", wcInsuranceTypeDTO);
        WcInsuranceType wcInsuranceType = wcInsuranceTypeMapper.toEntity(wcInsuranceTypeDTO);
        wcInsuranceType = wcInsuranceTypeRepository.save(wcInsuranceType);
        WcInsuranceTypeDTO result = wcInsuranceTypeMapper.toDto(wcInsuranceType);
        //wcInsuranceTypeSearchRepository.save(wcInsuranceType);
        return result;
    }

    /**
     *  Get all the wcInsuranceTypes.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<WcInsuranceTypeDTO> findAll(Pageable pageable) {
        log.debug("Request to get all WcInsuranceTypes");
        return wcInsuranceTypeRepository.findAll(pageable)
            .map(wcInsuranceTypeMapper::toDto);
    }

    /**
     *  Get one wcInsuranceType by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public WcInsuranceTypeDTO findOne(Long id) {
        log.debug("Request to get WcInsuranceType : {}", id);
        WcInsuranceType wcInsuranceType = wcInsuranceTypeRepository.findOne(id);
        return wcInsuranceTypeMapper.toDto(wcInsuranceType);
    }

    /**
     *  Delete the  wcInsuranceType by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete WcInsuranceType : {}", id);
        wcInsuranceTypeRepository.delete(id);
        //wcInsuranceTypeSearchRepository.delete(id);
    }

    /**
     * Search for the wcInsuranceType corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<WcInsuranceTypeDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of WcInsuranceTypes for query {}", query);
        Page<WcInsuranceType> result = wcInsuranceTypeSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(wcInsuranceTypeMapper::toDto);
    }
}
